import _ from 'lodash';
import { defineStore } from 'pinia';
import { getTokenFromCookie } from '@/utils/cookie-util';
import { localRead, localSave } from '@/utils/local-util';
export const useUserStore = defineStore({
    id: 'userStore',
    state: () => ({
      token: '',
      id:'',
      userName: '管理员',
      phone: '',
      mchId:0,
      nickName: '管理员',
      ipAddress: '',
      lastLogin: '',
      type:0,
      menuTree: [],
      menuRouterList: [],
      menuStr:[],
      menuRouterInitFlag: false,
      menuParentIdListMap: new Map(),
      pointsList: [],
      tagNav: [],
      keepAliveIncludes: [],
    }),
    getters: {
      getToken(state) {
        if (state.token) {
          return state.token;
        }
        return getTokenFromCookie();
      },
      //标签页
      getTagNav(state) {
        if (_.isEmpty(state.tagNav)) {
          let localTagNav = localRead('smart_admin_user_tag_nav') || '';
          state.tagNav = localTagNav ? JSON.parse(localTagNav) : [];
        }
        let tagNavList:Array<any> = _.cloneDeep(state.tagNav) || [];
        return tagNavList;
      }
    },
    actions:{
      setToken(token:string) {
        this.token = token;
      },
      logout() {
        this.tagNav = [];
      },
      //设置登录信息
      setUserLoginInfo(data:any) {
        this.userName = data.userName;
        this.phone = data.phone;
        this.nickName = data.nickName;
        this.type = data.type
        this.ipAddress = data.ipAddress;
        this.lastLogin = data.lastLogin;
      },
      setUserInfo(data:any) {
        this.userName = data.loginName;
        this.phone = data.tel;
        this.type = data.type;
        this.mchId = data.mchId
        this.nickName = data.nickname;
        this.ipAddress = data.ip;
        this.lastLogin = data.loginTime;
      },
      //设置标签页
      setTagNav(route:any, from:any) {
        if (_.isEmpty(this.getTagNav)) this.tagNav = [];
        let name = route.name;
        let findTag:any = (this.tagNav || []).find((e:any) => e.menuName == name);
        if(!findTag || findTag == 'undefined'){
          let routerData = {
            menuName: name,
            menuTitle: route.meta.title,
            menuQuery: route.query,
            fromMenuName: from.name,
            fromMenuQuery: from.query,
          }
          if(name == 'Home'){
            // @ts-ignore
            this.tagNav.unshift(routerData);
          }else{
            // @ts-ignore
            this.tagNav.push(routerData);
          }
          localSave('smart_admin_user_tag_nav', JSON.stringify(this.tagNav));
        }
      },
      //关闭标签页
      closeTagNav(menuName:any, closeAll:any) {
        if (_.isEmpty(this.getTagNav)) return;
        if (closeAll && !menuName) {
          this.tagNav = [];
          this.clearKeepAliveIncludes('');
        } else {
          let findIndex = (this.tagNav || []).findIndex((e:any) => e.menuName == menuName);
          if (closeAll) {
            if (findIndex == -1) {
              this.tagNav = [];
              this.clearKeepAliveIncludes('');
            } else {
              let tagNavElement = (this.tagNav || [])[findIndex];
              this.tagNav = [tagNavElement];
              // @ts-ignore
              this.clearKeepAliveIncludes(tagNavElement.menuName);
            }
          } else {
            (this.tagNav || []).splice(findIndex, 1);
            this.deleteKeepAliveIncludes(menuName);
          }
        }
        localSave('smart_admin_user_tag_nav', JSON.stringify(this.tagNav));
      },
      // 加入缓存
      pushKeepAliveIncludes(val:any) {
        if (!val) {
          return;
        }
        if (!this.keepAliveIncludes) {
          this.keepAliveIncludes = [];
        }
        if (this.keepAliveIncludes.length < 30) {
          let number = this.keepAliveIncludes.findIndex((e) => e === val);
          if (number === -1) {
            // @ts-ignore
            this.keepAliveIncludes.push(val);
          }
        }
      },
      // 删除缓存
      deleteKeepAliveIncludes(val:any) {
        if (!this.keepAliveIncludes || !val) {
          return;
        }
        let number = this.keepAliveIncludes.findIndex((e) => e === val);
        if (number !== -1) {
          this.keepAliveIncludes.splice(number, 1);
        }
      },
      // 清空缓存
      clearKeepAliveIncludes(val:any) {
        // @ts-ignore
        if (!val || !this.keepAliveIncludes?.includes(val)) {
          this.keepAliveIncludes = [];
          return;
        }
        // @ts-ignore
        this.keepAliveIncludes = [val];
      },
    }
})